require "rubygems"

if File.exist?("./lib/ship.rb")
	puts "Development mode, using file on lib dir"
	$LOAD_PATH << "./lib"
	require "ship.rb"
else
	puts "Using Ship gem ..."
	begin
		require "ship"
	rescue LoadError => e
		puts "Could not load Ship gem: #{e}"
		exit(1)
	end	
end	

correios	= Ship::Ship.providers[Ship::Ship::CORREIOS]
services = [
            correios.services[Ship::Correios::SEDEX_SEM_CONTRATO],
	          correios.services[Ship::Correios::PAC_SEM_CONTRATO]
           ]
format	= Ship::Correios.formats[Ship::Correios::CAIXA_PACOTE]

puts "Calculating using #{correios} and #{services.map {|s| s.name}.join(',')}"

shipping         = Ship::Shipping.new
shipping.from    = "15085520"
shipping.to      = "15130000"
shipping.weight  = 1
shipping.width   = 25
shipping.height  = 5
shipping.length  = 25
shipping.format  = format
shipping.service = services

begin
	costs = Ship::Ship.cost(correios,shipping)
	for service in services
		puts "#{service.name}\t= R$ #{costs[service.code.to_s]["Valor"]}"
	end
rescue Exception => e
	puts "ERROR: #{e}"
end

shipping.id = "SS123456789BR"
puts "\nTracking package id #{shipping.id} ..."

Ship::Ship.where(correios,shipping).each do |event|
	puts "#{event[:date]} - #{event[:desc]} - #{event[:status].downcase}"
end
